jQuery.inlineuserinfo = function()
{
	var	posts = _j('.post'),
		len = posts.length,
		info = _j('<div id="inlineuinfo"></div>'),
		api_url = 'http://leprosorium.ru/api/lepropanel/',
		user_url = 'http://leprosorium.ru/users/',
		faces_s = 'http://faces.leprosorium.com/image.php?cat=&image=',
		faces_b = 'http://faces.leprosorium.com/full.php?cat=&img=',
		loading = 'safari-extension://com.sergio.pachini.leprapanel-NKDKSQ698R/panel/assets/loading.gif',
		cache = [], event, net, delay_show, delay_hide;
	
	var style = _j('<style>#inlineuinfo {display: none; position: absolute; padding: 10px; background: #fff; -webkit-box-shadow: rgba(0, 0, 0, 0.3) 0px 3px 10px; border-radius: 5px; font-size: 10px; width: 300px; min-height: 100px; line-height: 140%} #inlineuinfo a {color: #000 !important} #inlineuinfo img {max-width: 120px} .inlineloading {background: #fff url("'+loading+'") center center no-repeat !important;}</style>');
	
	_j(document.head).append(style);
	
	info.appendTo(document.body).mouseleave(function(){ 
		clear_delay();
		delay_hide = setTimeout(function(){
			info.fadeOut();
		}, 500)
	});
	
	if (len) parse_links();
	
	function parse_links()
	{
		for (var i = len; i--;)
		{
			bind_event(posts[i]);
		}
	}
	
	function bind_event(post)
	{
		var link = _j(post).find('.dd .p a[href*="/users/"]'),
			uname = link.text(),
			uid = _j(post).attr('class').match(/u([0-9]+)/);
		
		uid = (uid) ? uid[1] : _j(post).find('.dd .p .u').attr('onclick').toString().match('([0-9]+)')[0];
			
		cache[uname] = {uid: uid, uname: uname};
			
		link.mouseenter(function(e){
			var uname = this.innerHTML; clear_delay();

			delay_show = setTimeout(function(){
				event = e; fetch_info(uname);
			}, 1000)
  		});
	}
	
	function fetch_info(uname)
	{
		var user = cache[uname];

		info.html('').css({left: (event.pageX - 5) +'px', top: (event.pageY - 5) +'px'}).addClass('inlineloading').show();
		
		if (cache[user.uid])
		{
			show_info(user.uid); return;
		}
		
		net = _j.getJSON(api_url+user.uid, function(json){
			_j.get(user_url+user.uname, function(data){
				
				var upage = _j(data),
					img = upage.find('.userpicholder .userpic td img'),
					bpic, spic;
				
				(img.length) ? (spic = bpic = img.attr('src')) : (spic = faces_s+user.uname+'.jpg', bpic = faces_b+user.uname+'.jpg');
				
				cache[user.uid] = 
				{
					uname : user.uname,
					real_name : upage.find('.userbasicinfo h3').text(),
					from : upage.find('.userego').text(),
					reg_date : json.regdate,
					karma : json.karma,
					rating : json.rating,
					vote_weight : json.vote_weigth,
					invited_by : json.invited_by,
					karma_to : json.karmavote,
					karma_from : json.hiskarmavote,
					posts : json.posts,
					comments : json.comments,
					spic : spic,
					bpic : bpic
				}
				
				show_info(user.uid);
			});
		});
	}
	
	function show_info(uid)
	{
		var data = cache[uid],
			link_comm = 'http://leprosorium.ru/users/'+data.uname+'/comments/',
			link_post = 'http://leprosorium.ru/users/'+data.uname+'/posts/';
			
		var html = 	'<div style="float:right"><a href="'+data.bpic+'" target="_blank"><img src="'+data.spic+'" /></a></div>'+
					'<div><a href="'+user_url+data.uname+'" target="_blank">'+data.uname+'</a> ('+uid+')'+
					'<br />'+data.real_name+'<br />'+data.from+'<br />Здесь с '+data.reg_date+'<br /><br />'+
					'Карма/рейтинг: '+data.karma+'/'+data.rating+'<br />'+
					'Посты/комментарии: <a href="'+link_post+'" target="_blank">'+data.posts+'</a>/<a href="'+link_comm+'" target="_blank">'+data.comments+'</a><br />'+
					'Карма вы/вам: '+data.karma_to+'/'+data.karma_from+'</div>';
		
		info.html(html).removeClass('inlineloading');
	}
	
	function clear_delay()
	{
		clearTimeout(delay_show); clearTimeout(delay_hide);
	}
	
	document.addEventListener("DOMNodeInserted", function(e){
		var obj = e.target; if (_j(obj).hasClass('post')) bind_event(obj);
	}, false);
}

_j(document).ready(function(){
	if (location.host == 'leprosorium.ru' || location.host == 'www.leprosorium.ru') _j.inlineuserinfo();
})
